﻿
(function (window) {
    function PortalMatrix() {
        this.initialize();
    }

    var p = PortalMatrix.prototype = new createjs.Container();
    p.Container_initialize = p.initialize;
    
    p.containera1 = null;
    p.containera2 = null;
    p.containera3 = null;
    p.containera4 = null;
    p.containera5 = null;
    p.arrContainera1 = null;
    p.arrContainera2 = null;
    p.arrContainera3 = null;
    p.arrContainera4 = null;
    p.arrContainera5 = null;

    p.containerLine = null;
  
    p.matrixArray = null;

    var that;
    
    p.initialize = function () {
        this.Container_initialize();
        that = this;
        this.containera1 = new createjs.Container();
        this.containera2 = new createjs.Container();
        this.containera2.x = 94 + 21;
        this.containera3 = new createjs.Container();
        this.containera3.x = 2 * (94 + 21);

        this.containera4 = new createjs.Container();
        this.containera4.x = 3 * (94 + 21);
        this.containera5 = new createjs.Container();
        this.containera5.x = 4 * (94 + 21);
        this.containerLine = new createjs.Container();
        this.containerLine.y = -10;
        this.containerLine.x = -18;
        
        this.arrContainera1 = new Array();
        this.arrContainera2 = new Array();
        this.arrContainera3 = new Array();
        this.arrContainera4 = new Array();
        this.arrContainera5 = new Array();

        this.addChild(this.containera1, this.containera2, this.containera3, this.containera4, this.containera5, this.containerLine);
       
        this.x = 18+47;
        this.y = 10 + 47;
         
      

    };

    p.init = function (arraySpin) {
        

        var i;
        var item;
        for (i = 0; i < 3; i++) {
            item = new Spell(arraySpin[i][0].Id);
            item.x = 0;
            item.y = i * 94 + i * 5;
            this.arrContainera1.push(item);
            this.containera1.addChild(item);
        }
        
        
        for (i = 0; i < 3; i++) {
            item = new Spell(arraySpin[i][1].Id);
            item.x = 0;
            item.y = i * 94 + i * 5;
            this.arrContainera2.push(item);
            this.containera2.addChild(item);

        }
        
        for (i = 0; i < 3; i++) {
            item = new Spell(arraySpin[i][2].Id);
            item.x = 0;
            item.y = i * 94 + i * 5;
            this.arrContainera3.push(item);
            this.containera3.addChild(item);

        }
        
        for (i = 0; i < 3; i++) {
            item = new Spell(arraySpin[i][3].Id);
            item.x = 0;
            item.IsMaxtrix = false;
            item.y = i * 94 + i * 5;
            this.arrContainera4.push(item);
            this.containera4.addChild(item);
        }

        for (i = 0; i < 3; i++) {
            item = new Spell(arraySpin[i][4].Id);
            item.x = 0;
            item.y = i * 94 + i * 5;
            this.arrContainera5.push(item);
            this.containera5.addChild(item);
        }
        
    };
    
    p.onSpin = function (array,data) {

        var i;
        var item;

        this.matrixArray = new Array();
        this.matrixArray.push(new Array());
        this.matrixArray.push(new Array());
        this.matrixArray.push(new Array());
       
        for (i = -50; i <= -48; i++) {
            item = new Spell(array[i + 50][0].Id);
            item.y = i * 94 + i * 5;
            item.IsMaxtrix = true;
            this.matrixArray[i + 50].push(item);
            this.containera1.addChild(item);
          
        }

        for (i = -47; i < 0; i++) {
            item = new Spell(PortalCommon.FloorSpell());
            item.y = i * 94 + i * 5;
            item.cache(-47, -47, 94, 94);
            this.arrContainera1.push(item);
            this.containera1.addChild(item);
        }
        //One
        for (i = -50; i <=-48; i++) {
            item = new Spell(array[i + 50][1].Id);
            item.y = i * 94 + i * 5;
            this.matrixArray[i + 50].push(item);
            this.containera2.addChild(item);
        }

        for (i = -47; i < 0; i++) {
            item = new Spell(PortalCommon.FloorSpell());
            item.y = i * 94 + i * 5;
            item.cache(-47, -47, 94, 94);
            this.arrContainera2.push(item);
            this.containera2.addChild(item);
        }
       
        //Two

        for (i = -50; i <=-48; i++) {
            item = new Spell(array[i + 50][2].Id);
            item.y = i * 94 + i * 5;
            this.matrixArray[i + 50].push(item);
            this.containera3.addChild(item);

        }
        for (i = -47; i < 0; i++) {
            item = new Spell(PortalCommon.FloorSpell());
            item.y = i * 94 + i * 5;
            item.cache(-47, -47, 94, 94);
            this.arrContainera3.push(item);
            this.containera3.addChild(item);

        }
        //Three
        for (i = -50; i <=-48; i++) {
            item = new Spell(array[i + 50][3].Id);
            item.y = i * 94 + i * 5;
            this.matrixArray[i + 50].push(item);
            this.containera4.addChild(item);

        }
        for (i = -47; i < 0; i++) {
            item = new Spell(PortalCommon.FloorSpell());
            item.y = i * 94 + i * 5;
            item.cache(-47, -47, 94, 94);
            this.arrContainera4.push(item);
            this.containera4.addChild(item);

        }
       
        //four  
        for (i = -50; i <=- 48; i++) {
            item = new Spell(array[i + 50][4].Id);
            item.y = i * 94 + i * 5;
            this.matrixArray[i + 50].push(item);
            this.containera5.addChild(item);
        }

        for (i = -47; i < 0; i++) {
            item = new Spell(PortalCommon.FloorSpell());
            item.y = i * 94 + i * 5;
            item.cache(-47, -47, 94, 94);
            this.arrContainera5.push(item);
            this.containera5.addChild(item);
        }

        createjs.Tween.get(this.containera1, { loop: false }).to({ y: -50 }, 500, createjs.Ease.backIn).call(floopSpin1);
        createjs.Tween.get(this.containera2, { loop: false }).wait(500).to({ y: -50 }, 500, createjs.Ease.backIn).call(floopSpin2);
        createjs.Tween.get(this.containera3, { loop: false }).wait(1000).to({ y: -50 }, 500, createjs.Ease.backIn).call(floopSpin3);
        createjs.Tween.get(this.containera4, { loop: false }).wait(1500).to({ y: -50 }, 500, createjs.Ease.backIn).call(floopSpin4);
        createjs.Tween.get(this.containera5, { loop: false }).wait(2000).to({ y: -50 }, 500, createjs.Ease.backIn).call(floopSpin5);
        
        createjs.Tween.get(this).wait(5100).call(function () {

             
            for (i = 0; i < that.arrContainera1.length; i++) {
                    var obj1 = that.arrContainera1[i];
                    that.containera1.removeChild(obj1);
            }

            for (i = 0; i < that.arrContainera2.length; i++) {
                var obj2 = that.arrContainera2[i];
                that.containera2.removeChild(obj2);
            }


            for (i = 0; i < that.arrContainera3.length; i++) {
                var obj3 = that.arrContainera3[i];
                that.containera3.removeChild(obj3);
            }
            
            for (i = 0; i < that.arrContainera4.length; i++) {
                var obj4 = that.arrContainera4[i];
                that.containera4.removeChild(obj4);
            }
            

            for (i = 0; i < that.arrContainera5.length; i++) {
                var obj5 = that.arrContainera5[i];
                that.containera5.removeChild(obj5);
            }
            that.arrContainera1.splice(0, that.arrContainera1.length);
            that.arrContainera2.splice(0, that.arrContainera2.length);
            that.arrContainera3.splice(0, that.arrContainera3.length);
            that.arrContainera4.splice(0, that.arrContainera4.length);
            that.arrContainera5.splice(0, that.arrContainera5.length);

            if (data.LineWin.length > 0) {
                for (var j = 0; j < data.LineWin.length; j++) {
                    that.containerLine.addChild(new WinLine(data.LineWin[j].line, data.LineWin[j].item, that.matrixArray, PortalCommon.GetLineData(data.LineWin[j].line), 3000 * j));
                }
            }
       
        }).wait(1000).call(function () {

            if (onRaise != undefined)
                onRaise(data);
       
        });
    };

    function floopSpin1() {
        
        createjs.Tween.get(that.containera1, { loop: false }).to({ y: 4700 + 250 + 20 + 5 }, 2000).to({ y: 4700 + 250 }, 100, createjs.Ease.bounceInOut);
    }
    
    function floopSpin2() {
        createjs.Tween.get(that.containera2, { loop: false }).to({ y: 4700 + 250 + 20 + 5 }, 2000).to({ y: 4700 + 250 }, 100, createjs.Ease.bounceInOut);
    }
    
    function floopSpin3() {
        createjs.Tween.get(that.containera3, { loop: false }).to({ y: 4700 + 250 + 20 + 5 }, 2000).to({ y: 4700 + 250}, 100, createjs.Ease.bounceInOut);
    }
    
    function floopSpin4() {
        createjs.Tween.get(that.containera4, { loop: false }).to({ y: 4700 + 250 + 20 + 5 }, 2000).to({ y: 4700 + 250}, 100, createjs.Ease.bounceInOut);
    }
    
    function floopSpin5() {
        createjs.Tween.get(that.containera5, { loop: false }).to({ y: 4700 + 250 + 20 + 5 }, 2000).to({ y: 4700 + 250}, 100, createjs.Ease.bounceInOut);
    }

    var onRaise;

    p.OnRaise = function(callback) {

        onRaise = callback;
        
    };
  
    window.PortalMatrix = PortalMatrix;

}(window));




